<?php

Yii::import('application.models._base.BaseMessage');

class Message extends BaseMessage
{
	public static function model($className=__CLASS__) {
		return parent::model($className);
	}

    public function getTopMessage($uid){
        $sql = "SELECT t1.*,t2.nick_name,t2.avatar_status,t2.avatar_public,t2.domain,t3.relation_status FROM (SELECT * FROM (
SELECT from_user_id,content,is_read,create_time FROM {{message}} WHERE to_user_id=:uid ORDER BY create_time DESC
) a GROUP BY from_user_id ORDER BY from_user_id) t1 LEFT JOIN {{user}} t2 ON t1.from_user_id = t2.id LEFT JOIN {{message_screen}} t3 ON t1.from_user_id = t3.friend_id ";
        $command = Yii::app()->db->createCommand($sql);
        $info = $command->query(array(':uid'=>$uid));
        return $info;
    }
    public function getTopCountMessage($uid){
        $sql = "SELECT COUNT(*) AS count FROM (SELECT * FROM (
SELECT from_user_id,content,is_read,create_time FROM {{message}} WHERE to_user_id=$uid AND is_read=0 ORDER BY create_time DESC
) a GROUP BY from_user_id ORDER BY from_user_id) t1 LEFT JOIN {{user}} t2 ON t1.from_user_id = t2.id LEFT JOIN {{message_screen}} t3 ON t1.from_user_id = t3.friend_id ";
        $command = Yii::app()->db->createCommand($sql);
        $info = $command->queryRow();
        return $info['count'];
    }
    //获取发给这个用户的所有邮件
    public function getUserMessage($uid,$group_id){
        $user_count = $this->getTopCountMessage($uid);
        //获取系统公告
        $system_count = $this->getSystemMessage($group_id,$uid);
        return $user_count+$system_count;
    }

    public function getSystemMessage($group_id,$uid){
        //检查该会员所在会员组 的系统公告,再查询message_data表. 是否有记录. 无则加入 未读NUM.
        $group_arr =  MessageGroup::model()->getMessageGroup($group_id);
        $group_num = 0;
        foreach ($group_arr as $group){
            $group_message_id = $group['id'];
            $result = MessageData::model()->find('group_message_id=:group_message_id and user_id=:user_id',array(':group_message_id'=>$group_message_id,':user_id'=>$uid));
            if(!$result){
                $group_num = 1;
                break;
            }
        }
        return $group_num;
    }
    /*
     * 你访问的userid   friend_id
     * 你的id           uid
     * 取得聊天记录  按照时间正序取出
     */
    public function getMessage($friend_id,$uid){
        $db = Yii::app()->db->createCommand();
        $info = $db
            ->from('{{message}}')
            ->where('from_user_id=:fid AND to_user_id=:uid OR from_user_id=:uid AND to_user_id=:fid',array(':fid'=>$friend_id,':uid'=>$uid))
            ->order('create_time desc')
            ->limit(20)
            ->queryAll();
        return $info;
    }
    public function createMessage($uid,$friend_id,$content){
        $db = new Message();
        $info = array(
            'from_user_id'=>$uid,
            'to_user_id'=>intval($friend_id),
            'content'=>CHtml::encode($content),
            'is_read'=>0,
            'create_time'=>time(),
        );
        $db->setAttributes($info);
        if($db->save()){
            return true;
        }else{
            return false;
        }
    }

    /**
     * @param $uid
     * @param $friend_id
     * 标记为已读
     */
    public function isRead($uid,$friend_id){
        $this->updateAll(array('is_read'=>1),'from_user_id=:from_user_id AND to_user_id=:to_user_id',array(':from_user_id'=>$friend_id,':to_user_id'=>$uid));
    }

}